package com.open.capacity.recuritment.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.open.capacity.common.exception.controller.ControllerException;
import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.model.CrmIndustry;
import com.open.capacity.common.web.Result;
import com.open.capacity.recuritment.Dto.IndustryDto;
import com.open.capacity.recuritment.Dto.SysUserExcel;
import com.open.capacity.recuritment.service.IIndustryService;
import lombok.Cleanup;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;

@RestController
@RequestMapping("/industry")
public class IndustryController {

    @Resource
    private IIndustryService industryService;


    /**
     * 查询行业
     * @param
     * @return
     */
    @PostMapping("/findIndustry")
    public Result findIndustry(@RequestBody Long pid) {
        try {
            Vector<CrmIndustry> vector = industryService.findIndustry(pid);
            return Result.succeedWith(vector, 200, "");
        } catch (Exception e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 查询行业职位
     *
     * @param pid
     * @return
     */
    @PostMapping("/findPosition")
    public Result findPositionByPid(@RequestBody Long pid) {
        try {
            Vector<CrmIndustry> vector = industryService.findPosition(pid);
            return Result.succeedWith(vector, 0, "");
        } catch (Exception e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 模糊查询行业职位待业人员
     *
     * @param
     * @return
     */
    @PostMapping("/selectUnemployed/{industry}/{position}/{employedStatus}/{page}/{limit}")
    public Result selectUnemployed(@PathVariable Long industry, @PathVariable Long position, @PathVariable Long employedStatus
            , @PathVariable Long page, @PathVariable Long limit) {
        try {
            System.out.println(industry);
            //查询出行业为条件的待业状态人员
            Vector<IndustryDto> vector = industryService.selectUnemployed(industry, position, employedStatus, page, limit);
            //以行业为条件查询
//            vector.forEach(v -> {
//                Long id = v.getId();
//                //装入map去循环查询sql uid在关联表里 查处出行业id为条件去查询行业表数据
//                Map<String, Object> map = new HashMap<>();
//                map.put("id", id);
//                System.out.println("mapssssssś" + map);
//                Vector<IndustryDto> vector2 = industryService.selectIndustry();
//            });

            return Result.succeedWith(vector, 0, "");
        } catch (Exception e) {
            e.printStackTrace();
            throw new ControllerException(e);
        }
    }

    /**
     * 导出行业待业就业人员数据
     * @return
     * @throws ControllerException
     */
    @PostMapping("/exportUnemployed")
    @PreAuthorize("hasAuthority('user:post/users/exportUser')")
    public void exportUser(@RequestParam Map<String, Object> params, HttpServletResponse response) {
        try {
            List<SysUserExcel> result = industryService.findAllUsers(params);
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=myExcel.xls");
            @Cleanup OutputStream ouputStream = null;
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("用户导出", "用户"),
                    SysUserExcel.class, result);
            ouputStream = response.getOutputStream();
            workbook.write(ouputStream);
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ControllerException(e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new ControllerException(e);
        }

    }
}
